package com.august.service.impl;

import com.august.service.CompareService;
import com.august.task.MySqlTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

@Service("compareService")
@Slf4j
public class CompareServiceImpl implements CompareService {
    @Autowired
    @Qualifier("mysql1JdbcTemplate")
    private JdbcTemplate mysql1JdbcTemplate;

    @Autowired
    @Qualifier("mysql2JdbcTemplate")
    private JdbcTemplate mysql2JdbcTemplate;

    @Override
    public List selectMySql1() {
        return  mysql1JdbcTemplate.queryForList("select * from student limit 0,1");
    }

    @Override
    public List selectMySql2() {
        return  mysql2JdbcTemplate.queryForList("select * from student limit 0,1");
    }


    @Override
    public int selectCount() {
        return mysql1JdbcTemplate.queryForObject("select count(*) from student", Integer.class).intValue();

    }

    @Override
    public List<HashVO> getMySqyList(){
        List<HashVO> list = new ArrayList<>();
        int pageNum=150000;

        int count = mysql1JdbcTemplate.queryForObject("select count(1) from student", Integer.class);
        int pages = count%pageNum == 0?(count/pageNum):(count/pageNum + 1);
        ExecutorService executor = Executors.newFixedThreadPool(20);
        List<FutureTask<List<HashVO>>> futureList = new ArrayList<>();
        for(int i = 0; i < pages; i++){
            int start = i*pageNum;
            int end = pageNum;
            if(end > count) end = count;
            MySqlTask task = new MySqlTask(start, end);
            FutureTask<List<HashVO>> future = new FutureTask<>(task);
            executor.submit(future);
            futureList.add(future);
        }
       // executor.shutdown();
        for(FutureTask<List<HashVO>> future:futureList){
            try {
                List<HashVO> listq = future.get();
                if(null != listq && !listq.isEmpty())
                    list.addAll(listq);
            }catch (InterruptedException e) {
                e.printStackTrace();
            }catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public void compare(HashMap hashMap,long beginTime) {
        int pageNum=150000;
        int count = mysql2JdbcTemplate.queryForObject("select count(1) from student", Integer.class);
        int pages = count%pageNum == 0 ?(count/pageNum):(count/pageNum + 1);
        ExecutorService executor = Executors.newFixedThreadPool(20);

        List<FutureTask> futureList = new ArrayList<>();

        for(int i = 0; i < pages; i++){
            int startIndex = i*pageNum;
            int end = pageNum;
            if(end > count) end = count;
        ProTask task = new ProTask(startIndex,end,hashMap);
        FutureTask futureTask = new FutureTask(task,null);
        futureList.add(futureTask);
        executor.submit(futureTask);
    }
       // executor.shutdown();
        for(FutureTask<List<HashVO>> future:futureList){
            try {
                future.get();
            }catch (InterruptedException e) {
                e.printStackTrace();
            }catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        long end = System.currentTimeMillis();

        log.info("15万数据{}耗时:",end-beginTime);
        //判断是否比对完成
//        while(true){
//            if(executor.isTerminated()){
//                System.out.println("执行成功!!");
//                break;
//            }
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
    }
}
